from datetime import datetime

from django.db import models
from DjangoUeditor.models import UEditorField


# Create your models here.
class GoodsCategory(models.Model):
    """商品分类"""
    category_choices = (
        (1, '一级分类'),
        (2, '二级分类'),
        (3, '三级分类'),
    )
    name = models.CharField(max_length=100, verbose_name='类别名称', help_text='类别名称')
    code = models.CharField(max_length=100, verbose_name='类别编码', help_text='类别编码')
    desc = models.CharField(max_length=200, verbose_name='描述', help_text='描述')
    category_type = models.IntegerField(default=1, choices=category_choices,
                                        verbose_name='分类级别',help_text="分类级别")
    parent_category = models.ForeignKey('self', on_delete=models.CASCADE, help_text="父目录",
                                        verbose_name='父类目级别', null=True, blank=True, related_name='sub_cat')
    is_tab = models.BooleanField(default=False, verbose_name='是否是tab', help_text='是否是tab')
    # is_nav = models.BooleanField(default=False, verbose_name='是否是导航')
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsCategoryBrand(models.Model):
    """商品分类品牌"""
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, related_name='brands',
                                 on_delete=models.CASCADE, verbose_name='商品分类')
    # brand = models.ForeignKey('Brand', on_delete=models.CASCADE, verbose_name='品牌')
    name = models.CharField(max_length=30, verbose_name='品牌名称', help_text="品牌名称")
    desc = models.CharField(max_length=200, verbose_name='品牌描述', help_text="品牌描述")
    image = models.ImageField(max_length=200, upload_to='brand/image', verbose_name='品牌图片', help_text="品牌图片")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '商品分类品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """
    商品
    """
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name='商品分类')
    good_sn = models.CharField(max_length=100, verbose_name='商品编号', help_text='商品编号')
    name = models.CharField(max_length=100, verbose_name='商品名称', help_text='商品名称')
    click_num = models.IntegerField(default=0, verbose_name='点击数', help_text='点击数')
    sold_num = models.IntegerField(default=0, verbose_name='销量', help_text='销量')
    fav_num = models.IntegerField(default=0, verbose_name='收藏数', help_text='收藏数')
    goods_num = models.IntegerField(default=0, verbose_name='库存', help_text='库存')
    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价格', help_text='市场价格')
    shop_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='本店价格', help_text='本店价格')
    goods_brief = models.TextField(max_length=200, verbose_name='商品简介', help_text='商品简介')
    goods_desc = UEditorField(verbose_name='商品详情', imagePath='goods/images/', filePath='goods/files/', help_text='商品详情', width=1000, height=300)
    ship_free = models.BooleanField(default=False, verbose_name='是否包邮', help_text='是否包邮')
    goods_front_image = models.ImageField(max_length=200, upload_to='goods/images/', verbose_name='商品封面图片',
                                           help_text='商品封面图片')
    is_new = models.BooleanField(default=False, verbose_name='是否新品', help_text='是否新品')
    is_hot = models.BooleanField(default=False, verbose_name='是否热卖', help_text='是否热卖')
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    # brand = models.ForeignKey('Brand', on_delete=models.CASCADE, verbose_name='品牌')
    # desc = models.CharField(max_length=200, verbose_name='商品描述', help_text='商品描述')
    # price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格', help_text='商品价格')
    # stock = models.IntegerField(default=100, verbose_name='商品库存', help_text='商品库存')
    # sales = models.IntegerField(default=0, verbose_name='商品销量', help_text='商品销量')
    # is_best = models.BooleanField(default=False, verbose_name='是否精品', help_text='是否精品')
    # is_on_sale = models.BooleanField(default=True, verbose_name='是否上架', help_text='是否上架')
    # image = models.ImageField(max_length=200, upload_to='goods/image', verbose_name='商品图片', help_text="商品图片")

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class IndexAd(models.Model):
    '''设置一级类目的广告位'''
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, related_name="category", null=True, blank=True, verbose_name='商品类目')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, related_name="goods" ,verbose_name='商品')

    class Meta:
        verbose_name = '首页商品类别广告'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name

class GoodsImage(models.Model):
    """
    商品轮播图片
    """
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品', related_name="images")
    image = models.ImageField(max_length=200, upload_to='goods/image', verbose_name='商品图片', help_text="商品图片")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '商品轮播图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Banner(models.Model):
    """
    首页轮播图
    """
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    name = models.CharField(max_length=100, verbose_name='轮播图名称', help_text='轮播图名称')
    image = models.ImageField(upload_to='banner', verbose_name='轮播图图片', help_text="轮播图图片")
    index = models.IntegerField(default=0, verbose_name='轮播图顺序', help_text='轮播图顺序')
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = '首页轮播图'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name